package com.example.haoyundaoyonghu.base;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import java.lang.reflect.Method;

public class Bar {

    /**
     * @author  Zander  @date 2020/12/17
     * 刘海屏适配   横屏（左右偏移）   竖屏（上下偏移）
     */

    public static boolean hasVivoNotch(Context context){

        final int VIVO_NOTCH = 0x00000020;  //是否有刘海 （官方返回得参照，不可变）
        boolean ret = false;

        /*通过反射获取是否有刘海屏*/
        try {
            Method method  = Class.forName("android.util.FtFeature").getMethod("isFeatureSupport",int.class);
            ret = (boolean) method.invoke(null,VIVO_NOTCH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * @author  Zander  @date 2020/12/17
     * 小米全面屏适配
     */
    public static boolean hasXiaoMi(Context context){

        try {
            Class clazz = Class.forName("android.os.SystemProperties");
            Method method = clazz.getMethod("getInt",String.class,int.class);
            if ((Integer) method.invoke(null,"ro.miui.notch",0) == 1){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  false;
    }


    /**
     * oppo
     */
    public static boolean hasOppo(Context context){
       return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }


    public static boolean hasHuawei(Context context){
        boolean ret = false;

        ClassLoader cl = context.getClassLoader();
        Class HwNotchSizeUtil = null;
        try {
            HwNotchSizeUtil = cl.loadClass("com.huawei.android.tuil.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            ret = (boolean) get.invoke(HwNotchSizeUtil);
            Toast.makeText(context, "是否有刘海"+ret, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ret;
    }

    public static void changStatusIconCollor(boolean setDark, Activity activity) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            View decorView = activity.getWindow().getDecorView();
            if(decorView != null){
                int vis = decorView.getSystemUiVisibility();
                if(setDark){
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else{
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }


    public static void setWindowStatusBarColor(Activity activity, int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

                //顶部状态栏
                window.setStatusBarColor(activity.getResources().getColor(colorResId));

                //底部导航栏
                window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
